package com.tjtt.tdm.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelToolExt {
	private String filePath;
	private POIFSFileSystem fs;    
	private XSSFWorkbook wb;    
	private XSSFSheet sheet;    
	private XSSFRow row;
	private FileInputStream is;
	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public ExcelToolExt(String _filePath)
	{
		try {  
			is = new FileInputStream(new File(_filePath));  //读取的文件路径 
			wb = new XSSFWorkbook(new BufferedInputStream(is)); 
		} 
		catch (IOException e) 
		{            
			e.printStackTrace();        
		} 
		sheet = wb.getSheetAt(0);
	}
	
	public String GetCellText(int rowIndex,int columnIndex){
		row = sheet.getRow(rowIndex);
		XSSFCell cell = row.getCell((short) columnIndex);
		String str = "";
		if (cell!=null){
			str = getStringCellValue(cell).trim() ;
		}
		return str;
	}
	
	public int GetRowCount()
    {
		return sheet.getLastRowNum();
    }
	
	public int GetColumnCount(){
		row = sheet.getRow(0);
		return row.getLastCellNum();
	}
	
	public void Save(){
		fs = null;
		wb = null;
		sheet = null;
		row = null;
		try {
			is.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
     * 获取单元格数据内容为字符串类型的数据
     * 
     * @param cell Excel单元格
     * @return String 单元格数据内容
     */
    private String getStringCellValue(XSSFCell cell) {
        String strCell = "";
        switch (cell.getCellType()) {
        case XSSFCell.CELL_TYPE_STRING:
            strCell = cell.getStringCellValue();
            break;
        case XSSFCell.CELL_TYPE_NUMERIC:
            strCell = String.valueOf(cell.getNumericCellValue());
            break;
        case XSSFCell.CELL_TYPE_BOOLEAN:
            strCell = String.valueOf(cell.getBooleanCellValue());
            break;
        case XSSFCell.CELL_TYPE_BLANK:
            strCell = "";
            break;
        default:
            strCell = "";
            break;
        }
        if (strCell.equals("") || strCell == null) {
            return "";
        }
        if (cell == null) {
            return "";
        }
        return strCell;
    }

    /**
     * 获取单元格数据内容为日期类型的数据
     * 
     * @param cell
     *            Excel单元格
     * @return String 单元格数据内容
     */
    private String getDateCellValue(XSSFCell cell) {
        String result = "";
        try {
            int cellType = cell.getCellType();
            if (cellType == XSSFCell.CELL_TYPE_NUMERIC) {
                Date date = cell.getDateCellValue();
                result = (date.getYear() + 1900) + "-" + (date.getMonth() + 1)
                        + "-" + date.getDate();
            } else if (cellType == XSSFCell.CELL_TYPE_STRING) {
                String date = getStringCellValue(cell);
                result = date.replaceAll("[年月]", "-").replace("日", "").trim();
            } else if (cellType == XSSFCell.CELL_TYPE_BLANK) {
                result = "";
            }
        } catch (Exception e) {
            System.out.println("日期格式不正确!");
            e.printStackTrace();
        }
        return result;
    }

    
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ExcelToolExt et = new ExcelToolExt("d:\\表C 管段.xlsx");
		System.out.print(et.GetCellText(1, 1));
		et.Save();
	}

}
